package com.prince.web.service.impl;

import com.prince.bean.CommonWebResponse;
import com.prince.bean.ResultParams;
import com.prince.constant.Constants;
import com.prince.mapper.SalesRecordMapper;
import com.prince.model.SalesRecord;
import com.prince.model.SalesRecordExample;
import com.prince.page.Page;
import com.prince.util.DateUtil;
import com.prince.util.MapUtil;
import com.prince.web.service.SalesRecordService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 功能描述: 销售商品明细
 * @Author: 王培喜 58528792@163.com
 * @Date: 2021/4/8 14:48
 */
@Service
public class SalesRecordServiceImpl implements SalesRecordService {

    @Resource
    private SalesRecordMapper salesRecordMapper;
    /**
     * 功能描述: 分页查询销售明细
     * @Author: 王培喜 58528792@163.com
     * @Date: 2021/4/6 15:25
     */
    @Override
    public Map<String, Object> findSalesRecordByPage(Map<String, Object> params, Page page) {
        ResultParams response = CommonWebResponse.getSuccessResponseCodeMsgParams();

        SalesRecordExample example = new SalesRecordExample();
        SalesRecordExample.Criteria criteria = example.createCriteria().andDelEqualTo(Constants.YES);

        //封装条件
        if (MapUtil.isNotEmpty(params, "startTime")) {
            Date startTime = DateUtil.strToDate(params.get("startTime").toString(), "yyyy-MM-dd hh:mm:ss");
            Date endTime = DateUtil.strToDate(params.get("endTime").toString(), "yyyy-MM-dd hh:mm:ss");
            criteria.andCreatedBetween(startTime, endTime);
        }
        //订单号
        if(MapUtil.isNotEmpty(params,"orderId")){
            criteria.andOrderIdEqualTo(Integer.valueOf(params.get("orderId").toString()));
        }
        //商品类型
        if(MapUtil.isNotEmpty(params,"type")){
            criteria.andTypeEqualTo(Integer.valueOf(params.get("type").toString()));
        }
        //1  销售  2 退货
        if(MapUtil.isNotEmpty(params,"direction")){
            criteria.andDirectionEqualTo(Integer.valueOf(params.get("direction").toString()));
        }
        if(MapUtil.isNotEmpty(params,"name")){
            criteria.andNameLike("%"+params.get("name")+"%");
        }

        if(MapUtil.isNotEmpty(params,"productUnit")){
            criteria.andProductUnitEqualTo(params.get("productUnit").toString());
        }
        if(MapUtil.isNotEmpty(params,"specifications")){
            criteria.andSpecificationsEqualTo(params.get("specifications").toString());
        }
        if(MapUtil.isNotEmpty(params,"supplier")){
            criteria.andSupplierLike("%"+params.get("supplier")+"%");
            SalesRecordExample.Criteria supplier = example.createCriteria().andRemarkLike("%" + params.get("supplier") + "%");
            example.or(supplier);
        }

        int total = salesRecordMapper.countByExample(example);
        if(null != page){
            example.setPage(page);
        }
        example.setOrderByClause(" created desc ");
        List<SalesRecord> list = salesRecordMapper.selectByExample(example);
        return response.toHashMapByPage(total,list);
    }
    /**
     * 功能描述: 通过id查询订单明细
     * @Author: 王培喜 58528792@163.com
     * @Date: 2021/4/6 15:25
     */
    @Override
    public Map<String, Object> findOrderDetails(Map<String, Object> params, Page page) {
        ResultParams response = CommonWebResponse.getSuccessResponseCodeMsgParams();
        SalesRecordExample example = new SalesRecordExample();
        SalesRecordExample.Criteria criteria = example.createCriteria().andDelEqualTo(Constants.YES);

        if(MapUtil.isNotEmpty(params,"id")){
            criteria.andOrderIdEqualTo(Integer.valueOf(params.get("id").toString()));
        }
        int total = salesRecordMapper.countByExample(example);
        if(null != page){
            example.setPage(page);
        }
        example.setOrderByClause(" created desc ");
        List<SalesRecord> list = salesRecordMapper.selectByExample(example);
        return response.toHashMapByPage(total,list);
    }
}
